package com.geostar.constructionland.statistics.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.geostar.constructionland.common.entity.response.ExtJsAjaxResponse;
import com.geostar.constructionland.common.entity.statistics.StatisticsProfile;
import com.geostar.constructionland.statistics.service.StatisticsProfileService;
import com.geostar.constructionland.statistics.service.StatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * @Author makoto_zhu
 * @Date 2020-11-06 02:21:03
 * @Description
 **/
@Api("统计接口")
@RestController
@RequestMapping(path = "/statisticsProfile")
@Slf4j
@Validated
public class StatisticsController {

    @Autowired
    private StatisticsService statisticsService;

    @Autowired
    private StatisticsProfileService statisticsProfileService;

    @ApiOperation("启动统计服务")
    @PostMapping(path = "/executeStatement")
    public ExtJsAjaxResponse<String> getStatistic(@Valid @RequestParam @NotEmpty String uniqueTaskId,
                                                  @Valid @RequestParam @NotEmpty String ywid) {
        String result = statisticsService.getStatistic(uniqueTaskId, ywid);
        return ExtJsAjaxResponse.createBySuccess(result);
    }


    /**
     * 分页获取统计配置信息列表
     *
     * @param pageNo  当前页
     * @param pageNum 每页条数
     * @return
     */
    @PostMapping(path = "/list")
    public ExtJsAjaxResponse list(
            @NotNull(message = "页码不能为空") @RequestParam(value = "page") int pageNo,
            @NotNull(message = "每页数量不能为空") @RequestParam(value = "limit") int pageNum, String query_param_statistics_name) {
        IPage<StatisticsProfile> analyseVersionIPage
                = statisticsProfileService.getStatisticProfilesForPage(pageNo, pageNum, query_param_statistics_name);
        return ExtJsAjaxResponse.createBySuccess(analyseVersionIPage);
    }

    @ApiOperation("删除统计配置实体")
    @PostMapping(path = "/delete")
    public ExtJsAjaxResponse delete(@NotBlank(message = "id不能为空") String id) {
        statisticsProfileService.deleteById(id);
        return ExtJsAjaxResponse.createBySuccessMessage("删除统计配置成功");
    }

    @PostMapping(path = "/add")
    @ApiOperation("保存统计配置实体")
    public ExtJsAjaxResponse add(@RequestBody @Valid StatisticsProfile statisticsProfile) {
        statisticsProfile.setModifiedTime(new Date());
        int count = statisticsProfileService.getBaseMapper().selectList(new LambdaQueryWrapper<StatisticsProfile>()
                .eq(StatisticsProfile::getUniqueTaskId, statisticsProfile.getUniqueTaskId()))
                .size();
        if (count > 0) {
            return ExtJsAjaxResponse.createByErrorMessage("UniqueTaskId already exist");
        }
        boolean message = statisticsProfileService.save(statisticsProfile);
        return ExtJsAjaxResponse.createBySuccess(message);
    }


    @PostMapping(path = "/update")
    @ApiOperation("保存统计配置实体")
    public ExtJsAjaxResponse update(@RequestBody @Valid StatisticsProfile statisticsProfile) {
        statisticsProfile.setModifiedTime(new Date());
        int count = statisticsProfileService.getBaseMapper()
                .selectList(
                        new LambdaQueryWrapper<StatisticsProfile>()
                                .eq(StatisticsProfile::getUniqueTaskId, statisticsProfile.getUniqueTaskId())
                                .ne(StatisticsProfile::getStatisticsVersion, statisticsProfile.getStatisticsVersion()))
                .size();
        if (count > 0) {
            return ExtJsAjaxResponse.createByErrorMessage("UniqueTaskId already exist");
        }
        boolean message = statisticsProfileService.updateById(statisticsProfile);
        return ExtJsAjaxResponse.createBySuccess(message);
    }
}
